package com.yn.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yn.common.exception.TransitException;
import com.yn.common.model.zyorder.ZyOrderServiceT;
import com.yn.common.model.zyorder.ZyServiceConfig;
import com.yn.common.model.zyorder.ZyServiceRegionPrice;
import com.yn.common.page.BasePage;
import com.yn.common.request.sc.PageParamRequest;
import com.yn.common.request.zyorder.ZyServiceConfigRequest;
import com.yn.common.request.zyorder.ZyServiceConfigSearch;
import com.yn.common.response.zyorder.ZyServiceConfigResponse;
import com.yn.service.dao.zy.ZyServiceConfigDao;
import com.yn.service.service.ZyOrderServiceService;
import com.yn.service.service.ZyServiceConfigService;
import com.yn.service.service.ZyServiceRegionPriceService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* 转运订单 接口实现
 *
*/
@Service

public class ZyServiceConfigServiceImpl extends ServiceImpl<ZyServiceConfigDao, ZyServiceConfig> implements ZyServiceConfigService {

    private Logger logger = LoggerFactory.getLogger(ZyServiceConfigServiceImpl.class);

    @Autowired
    private ZyServiceRegionPriceService zyServiceRegionPriceService;

    @Autowired
    private ZyOrderServiceService zyOrderServiceService;
    /**
     * 服务项配置新增或修改
     */
    @Override
    public void saveOrUpdateServiceConfig(ZyServiceConfigRequest zyServiceConfigRequest) {
        ZyServiceConfig zyServiceConfig = new ZyServiceConfig();
        //名称重复检验
        boolean b1 = repeatVerification(zyServiceConfigRequest);
        if(!b1){
            logger.error("服务项配置名称重复：{}",zyServiceConfigRequest);
            throw new TransitException("服务项配置名称重复");
        }
        BeanUtil.copyProperties(zyServiceConfigRequest,zyServiceConfig);
        boolean b = this.saveOrUpdate(zyServiceConfig);
        if(!b){
            logger.error("服务项配置新增或修改失败：{}",zyServiceConfigRequest);
            throw new TransitException("服务项配置新增或修改失败");
        }
    }

    @Override
    public ZyServiceConfigResponse getServiceConfigById(Integer id) {
        ZyServiceConfig byId = this.getById(id);
        if(byId != null){
            ZyServiceConfigResponse zyServiceConfigResponse = new ZyServiceConfigResponse();
            BeanUtil.copyProperties(byId,zyServiceConfigResponse);
            Integer serviceUnionType = zyServiceConfigResponse.getServiceUnionType();
            if(serviceUnionType == 1){
                zyServiceConfigResponse.setServiceUnionTypeName("范围");
            } else if(serviceUnionType == 2){
                zyServiceConfigResponse.setServiceUnionTypeName("单价");
            }
            Integer serviceType = zyServiceConfigResponse.getServiceType();
            if(serviceType == 1){
                zyServiceConfigResponse.setServiceTypeName("转运");
            }
            else if(serviceType == 2){
                zyServiceConfigResponse.setServiceTypeName("普通服务");
            }
            return zyServiceConfigResponse;
        }
        return null;
    }

    @Override
    public void del(Integer id) {
        LambdaQueryWrapper<ZyServiceRegionPrice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZyServiceRegionPrice::getServiceConfigId,id);
        queryWrapper.eq(ZyServiceRegionPrice::getIsDelTag,false);
        List<ZyServiceRegionPrice> list = zyServiceRegionPriceService.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(list)){
            logger.error("该服务项配置下存在区域价格，不能删除 配置id:{}",id);
            throw new TransitException("该服务项配置下存在区域价格配置信息，不能删除");
        }
        LambdaQueryWrapper<ZyOrderServiceT> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(ZyOrderServiceT::getServiceConfigId,id);
        queryWrapper1.eq(ZyOrderServiceT::getIsDelTag,false);
        List<ZyOrderServiceT> zyOrderServiceTList = zyOrderServiceService.list(queryWrapper1);
        if (CollectionUtils.isNotEmpty(zyOrderServiceTList)){
            logger.error("该服务项配置下存在订单服务,不能删除 配置id:{}",id);
            throw new TransitException("该服务项配置下存在订单服务信息，不能删除");
        }
         this.removeById(id);
    }

    @Override
    public BasePage<ZyServiceConfigResponse> listServiceConfigByServiceTypeList(ZyServiceConfigSearch serviceConfigSearch,
                                                                               PageParamRequest pageParamRequest) {
        Page<Object> startPage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<ZyServiceConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(serviceConfigSearch.getServiceType()!=null,ZyServiceConfig::getServiceType,
                serviceConfigSearch.getServiceType());
        queryWrapper.eq(serviceConfigSearch.getServiceUnionType()!=null,ZyServiceConfig::getServiceUnionType,
                serviceConfigSearch.getServiceUnionType());

        queryWrapper.like(StrUtil.isNotBlank(serviceConfigSearch.getServiceName()),ZyServiceConfig::getServiceName,
                serviceConfigSearch.getServiceName());
        queryWrapper.orderByDesc(ZyServiceConfig::getId);
        List<ZyServiceConfig> zyServiceConfigList = this.list(queryWrapper);
        List<ZyServiceConfigResponse> zyServiceConfigResponseList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(zyServiceConfigList)){
            //数据处理
            formatOrder1(zyServiceConfigList,zyServiceConfigResponseList);
        }
        return BasePage.restPage(BasePage.copyPageInfo(startPage, zyServiceConfigResponseList));
    }

    void formatOrder1(List<ZyServiceConfig> zyServiceConfigList,List<ZyServiceConfigResponse> zyServiceConfigResponseList) {
        zyServiceConfigList.parallelStream().forEach(zyServiceConfig -> {
            ZyServiceConfigResponse zyServiceConfigResponse = new ZyServiceConfigResponse();
            BeanUtil.copyProperties(zyServiceConfig,zyServiceConfigResponse);
            Integer serviceUnionType = zyServiceConfigResponse.getServiceUnionType();
            if(serviceUnionType == 1){
                zyServiceConfigResponse.setServiceUnionTypeName("范围");
            } else if(serviceUnionType == 2){
                zyServiceConfigResponse.setServiceUnionTypeName("单价");
            }
            Integer serviceType = zyServiceConfigResponse.getServiceType();
            if(serviceType == 1){
                zyServiceConfigResponse.setServiceTypeName("转运");
            }
            else if(serviceType == 2){
                zyServiceConfigResponse.setServiceTypeName("普通服务");
            }
            zyServiceConfigResponseList.add(zyServiceConfigResponse);
        });
    };

    //重复验证
    public boolean repeatVerification(ZyServiceConfigRequest zyServiceConfigRequest) {
        Integer id = zyServiceConfigRequest.getId();
        LambdaQueryWrapper<ZyServiceConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZyServiceConfig::getServiceName,zyServiceConfigRequest.getServiceName());
        queryWrapper.eq(ZyServiceConfig::getServiceType,zyServiceConfigRequest.getServiceType());
        ZyServiceConfig one = this.getOne(queryWrapper);
        if(one!= null){
            if(id == null){
                return false;
            }
            if(!id.equals(one.getId())){
                return false;
            }
        }

        return true;
    }
}

